using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

using CYZHAPI.Common;
using CYZHAPI.Entity;
using CYZHAPI.IRepository.IService;
using CYZHAPI.Middlewares;

using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using CYZHAPI.web.Model;
using Microsoft.Extensions.Options;
using CYZHAPI.Common.Config;
namespace CYZHAPI.web.Controllers
{
    public class rasterResultController : BaseController
    {
         private WebApiOption _Config;
        private IRasterResultService _rasterService;
        public rasterResultController(IOptions<WebApiOption> option,IRasterResultService rasterService, ILogger<BaseController> logger) : base(logger)
        {
             _Config = option.Value;
            _rasterService = rasterService;
        }

        [Route("Seach")]
        [HttpPost]
        public RasterRenBean Seach([FromBody]RasterSeachBean value)
        {
            value.algorithm = value.algorithm == null ? "" : value.algorithm;
            value.year = value.year == null ? "" : value.year;
            value.fileName = value.fileName == null ? "" : value.fileName;
            value.mouth = value.mouth == null ? "" : value.mouth;
            value.xun = value.xun == null ? "" : value.xun;
           value.XzqhCode = value.XzqhCode == null ? "" : value.XzqhCode;
            string sWhere = "";
            if (!value.algorithm.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "ALGORITHM = '" + value.algorithm + "'" : " and ALGORITHM = '" + value.algorithm + "'";
            }
            if (!value.XzqhCode.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "SCOPE = '" + value.XzqhCode + "'" : " and SCOPE = '" + value.XzqhCode + "'";
            }
            if (!value.year.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "year = '" + value.year + "'" : " and year = '" + value.year + "'";

                if(!value.mouth.Equals("")){
                     
                     if(!value.xun.Equals("")){
                         //按旬合成，查询类型为days的数据
                         var startTime="";
                         var endTime="";
                         if(value.xun.Equals("01")){
                             startTime=value.year+"-"+value.mouth+"-01";
                             endTime=value.year+"-"+value.mouth+"-10";
                         }else if(value.xun.Equals("02")){
                            startTime=value.year+"-"+value.mouth+"-11";
                             endTime=value.year+"-"+value.mouth+"-20";
                         }else{
                             //计算某个月的天数
                             int days = DateTime.DaysInMonth(Convert.ToInt32(value.year), Convert.ToInt32(value.mouth)); 
                         
                             startTime=value.year+"-"+value.mouth+"-20";
                             endTime=value.year+"-"+value.mouth+"-"+days;
                         }
                         
                         
                         DateTime startdt=Convert.ToDateTime(startTime);
                         DateTime enddt=Convert.ToDateTime(endTime);
                         
                         var startday=startdt.DayOfYear;
                         var endday=enddt.DayOfYear;
                         sWhere += " and days >= "+startday+" and days<="+endday ;
                         sWhere += " and timetype = 'days'";

                     }else{
                         //按月合成，查询类型为xun的数据
                         sWhere +=" and mouth = '" + value.mouth + "'";
                         sWhere += " and timetype = 'xun'";
                     }
                }else{
                    sWhere += " and timetype = 'mouth'";
                }
            }else{
                //查询年的数据
                sWhere += sWhere.Equals("") ? "timetype = 'year'" : " and timetype = 'year'";

            }
            if(sWhere.Equals("")){
                sWhere +="id>0";
            }

            List<RasterResultsModel> ListInfo = _rasterService.GetList(sWhere);
            RasterRenBean renBean = new RasterRenBean();
            if (ListInfo != null)
            {
                renBean.code = "200";
                renBean.message = "数据查询成功";
                renBean.reList = ListInfo;
            }
            else
            {
                renBean.code = "901";
                renBean.message = "数据查询失败";
                renBean.reList = null;
            }
            return renBean;
        }
        [Route("SeachAll")]
        [HttpPost]
        public RasterRenBean SeachAll([FromBody]RasterSeachBean value)
        {
           string sWhere="";
            if(sWhere.Equals("")){
                sWhere +="id>0";
            }

            List<RasterResultsModel> ListInfo = _rasterService.GetList(sWhere);
            RasterRenBean renBean = new RasterRenBean();
            if (ListInfo != null)
            {
                renBean.code = "200";
                renBean.message = "数据查询成功";
                renBean.reList = ListInfo;
            }
            else
            {
                renBean.code = "901";
                renBean.message = "数据查询失败";
                renBean.reList = null;
            }
            return renBean;
        }
        [Route("SeachSrcByDays")]
        [HttpPost]
        public RasterRenBean SeachSrcByDays([FromBody]RasterSeachBean value)
        {
            value.algorithm = value.algorithm == null ? "" : value.algorithm;
            value.year = value.year == null ? "" : value.year;
            value.fileName = value.fileName == null ? "" : value.fileName;
            value.mouth = value.mouth == null ? "" : value.mouth;
            value.xun = value.xun == null ? "" : value.xun;
           value.XzqhCode = value.XzqhCode == null ? "" : value.XzqhCode;
            string sWhere = "";
            if (!value.algorithm.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "ALGORITHM = '" + value.algorithm + "'" : " and ALGORITHM = '" + value.algorithm + "'";
            }
            if (!value.XzqhCode.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "SCOPE = '" + value.XzqhCode + "'" : " and SCOPE = '" + value.XzqhCode + "'";
            }
            if (!value.year.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "year = '" + value.year + "'" : " and year = '" + value.year + "'";

            }
            if (!value.days.Equals(""))
            {
                sWhere += sWhere.Equals("") ? "days = " + value.days  : " and days = " + value.days ;
                sWhere +=" and timetype='days'";
            }
            

            List<RasterResultsModel> ListInfo = _rasterService.GetList(sWhere);
            RasterRenBean renBean = new RasterRenBean();
            if (ListInfo != null)
            {
                renBean.code = "200";
                renBean.message = "数据查询成功";
                renBean.reList = ListInfo;
            }
            else
            {
                renBean.code = "901";
                renBean.message = "数据查询失败";
                renBean.reList = null;
            }
            return renBean;
        }
        [Route("SeachNDVILST")]
        [HttpPost]
        public RasterRenBean SeachNDVILST([FromBody]RasterSeachBean value)
        {
            string[] filetime=value.year.Split('-');
            string sWhere = "ALGORITHM in ('NDVI','LST') and SCOPE='"+value.XzqhCode + "' and timetype='xun'";
            sWhere+=" and year="+filetime[0]+" and mouth="+filetime[1]+" and xun="+filetime[2];
            
            List<RasterResultsModel> ListInfo = _rasterService.GetList(sWhere);
            RasterRenBean renBean = new RasterRenBean();
            if (ListInfo != null)
            {
                renBean.code = "200";
                renBean.message = "数据查询成功";
                renBean.reList = ListInfo;
            }
            else
            {
                renBean.code = "901";
                renBean.message = "数据查询失败";
                renBean.reList = null;
            }
            return renBean;
        }
        [Route("Delete")]
        [HttpGet("{id}")]
        public BaseResponseResult Delete(int id)
        {
           // id = id == null ? "" : id;
           // var sid=Convert.ToInt32(id);
            BaseResponseResult response = new BaseResponseResult();
            bool e = _rasterService.Delete(id);
            if(e){
                response = new BaseResponseResult
                {
                    code = "200",
                    message = "数据删除成功!"
                };
            }else{
                response = new BaseResponseResult
                {
                    code = "901",
                    message = "数据删除错误!"
                };
            }
            return response;
        }
        [Route("downLoadFile")]
        [HttpGet]
        public async Task<IActionResult> downLoadFile(string fileid){
            
            var sWhere="id="+fileid;
            var path=_Config.Path.basePath;
            List<RasterResultsModel> ListInfo = _rasterService.GetList(sWhere);
            if(ListInfo != null&&ListInfo.Count==1){
                RasterResultsModel srm=ListInfo[0];
                 var fileType="";
                 if(srm.algorithm.Equals("SPI")){
                     fileType="ASC";
                 }else if(srm.algorithm.Equals("NDVI")||srm.algorithm.Equals("GRASS")){
                     fileType="MOD09Q1";
                 }else if(srm.algorithm.Equals("LST")||srm.algorithm.Equals("TVDI")){
                     fileType="MOD11A1";
                 }
                 var addrUrl=path+fileType+"\\result\\"+srm.algorithm+"\\"+srm.filename+".tif";
                var stream = System.IO.File.OpenRead(addrUrl);
                return File(stream, "application/octet-stream", srm.filename+".tif");
            }else{
                return null;
            }
           
        }

    }
}